package com.zyj.benchmark.sample;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 对 @Group @GroupThreads 的介绍
 */
@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
@State(Scope.Group)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class Sample_15_Asymmetric {

    private AtomicInteger counter;

    @Setup
    public void up() {
        counter = new AtomicInteger(0);
    }

    @Benchmark
    @Group("benchmark1_thread_competition")
    @GroupThreads(3)
    public int inc() {
        return counter.incrementAndGet();
    }

    @Benchmark
    @Group("benchmark1_thread_competition")
    @GroupThreads(1)
    public int get() {
        return counter.get();
    }

    //对照组
    @Benchmark
    @Group("benchmark2_inc_no_competition")
    @GroupThreads(1)
    public int inc1() {
        return counter.incrementAndGet();
    }

    @Benchmark
    @Group("benchmark3_get_no_competition")
    public int get1() {
        return counter.get();
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(Sample_15_Asymmetric.class.getSimpleName())
                .forks(1)
                .build();

        new Runner(opt).run();
    }
}

//Benchmark                                                Mode  Cnt   Score   Error   Units
//Sample_15_Asymmetric.benchmark1_thread_competition      thrpt       73.985          ops/us
//Sample_15_Asymmetric.benchmark1_thread_competition:get  thrpt       34.700          ops/us
//Sample_15_Asymmetric.benchmark1_thread_competition:inc  thrpt       39.285          ops/us


//单独执行get
//Benchmark                                            Mode  Cnt    Score   Error   Units
//Sample_15_Asymmetric.benchmark1_thread_competition  thrpt       314.422          ops/us


//单独执行inc, 1个线程
//Benchmark                                            Mode  Cnt   Score   Error   Units
//Sample_15_Asymmetric.benchmark1_thread_competition  thrpt       99.038          ops/us

//单独执行inc, 3个线程
//Benchmark                                            Mode  Cnt   Score   Error   Units
//Sample_15_Asymmetric.benchmark1_thread_competition  thrpt       50.155          ops/us







